package com.yida.gwj.expend.outside.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessBuild;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessHouse;
import com.yida.gwj.expend.outside.domain.dto.AcceptanceReportDTO;
import com.yida.gwj.expend.outside.domain.dto.AllocatedAmountListDTO;
import com.yida.gwj.expend.outside.domain.dto.AllocatedAmountSelectDTO;
import com.yida.gwj.expend.outside.domain.dto.ApplyAppropriateReportDTO;
import com.yida.gwj.expend.outside.domain.dto.GetBusinessDetailDTO;
import com.yida.gwj.expend.outside.domain.dto.GwjMaintenanceFundsBusinessListDTO;
import com.yida.gwj.expend.outside.domain.dto.StepConfigInfoDTO;
import com.yida.gwj.expend.outside.domain.dto.TotalCostShareListDTO;
import com.yida.gwj.expend.outside.domain.request.AllocatedAmountRequest;
import com.yida.gwj.expend.outside.domain.request.GwjMaintenanceFundsBusinessListRequest;
import com.yida.gwj.expend.outside.domain.request.HousesUnderBuildRequest;
import com.yida.gwj.expend.outside.domain.request.SaveAppropriateApplyRequest;
import com.yida.gwj.expend.outside.domain.request.ShareEquallyMoneyRequest;
import com.yida.gwj.expend.outside.enums.BusinessTypeEnum;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessService;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessUrgentAppropriateService;

import cn.hutool.core.io.IORuntimeException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 公共维修资金应急拨付申请业务
 *
 * @author wkz
 * @date 2023/04/10
 */
@RestController
@RequestMapping("/expend/outside/urgent/appropriate")
// @Api(tags = "公维金维修资金应急拨付业务")
@Api(tags = "公共维修资金应急拨付申请业务")
public class GwjMaintenanceFundsBusinessUrgentAppropriateController extends BaseController {

    @Autowired
    private IGwjMaintenanceFundsBusinessUrgentAppropriateService gwjMaintenanceFundsBusinessUrgentAppropriateService;

    // 20230731
    @Autowired
    private IGwjMaintenanceFundsBusinessService gwjMaintenanceFundsBusinessService;

    /**
     * 查询公维金拨付申请业务列表
     */
    @ApiOperation(value = "查询公维金拨付申请业务列表", response = GwjMaintenanceFundsBusinessListDTO.class)
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:list")
    @GetMapping("/list")
    public TableDataInfo list(GwjMaintenanceFundsBusinessListRequest maintenanceFundsBusinessAppropriateListRequest) {
        startPage();
        maintenanceFundsBusinessAppropriateListRequest.setApplicantUserId(SecurityUtils.getLoginUser().getUserid());
        maintenanceFundsBusinessAppropriateListRequest.setType(BusinessTypeEnum.URGENT_APPROPRIATE_PREPARE.getCode());
        List<GwjMaintenanceFundsBusinessListDTO> list = gwjMaintenanceFundsBusinessUrgentAppropriateService.getGwjMaintenanceFundsBusinessList(maintenanceFundsBusinessAppropriateListRequest);
        return getDataTable(list);
    }

    /**
     * 获取公维金拨付申请业务详细信息
     */
    @ApiOperation(value = "获取公维金拨付申请业务详细信息", response = GetBusinessDetailDTO.class)
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:list")
    @GetMapping(value = "/business/detail/{bizId}")
    public AjaxResult getInfo(@PathVariable("bizId") Long bizId) {
        return AjaxResult.success(gwjMaintenanceFundsBusinessUrgentAppropriateService.getDetailMessage(bizId));
    }

    /**
     * 分摊金额列表
     */
    @ApiOperation(value = "拨付分摊范围及金额-选中房屋列表", response = AllocatedAmountSelectDTO.class)
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:edit")
    @PostMapping("/allocated/amount/list")
    public AjaxResult allocatedAmountList(@Validated @RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        startPage();
        return AjaxResult.success(gwjMaintenanceFundsBusinessUrgentAppropriateService.allocatedAmountList(allocatedAmountRequest));
    }

    @ApiOperation(value = "分摊范围及金额-未选中房屋列表", response = GwjMaintenanceFundsBusinessHouse.class)
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:list")
    @PostMapping("/allocated/amount/cancel/list")
    public TableDataInfo allocatedAmountCancelList(@Validated @RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        startPage();
        List<GwjMaintenanceFundsBusinessHouse> list = gwjMaintenanceFundsBusinessUrgentAppropriateService.allocatedAmountCancelList(allocatedAmountRequest);
        return getDataTable(list);
    }

    /**
     * 按类型平摊金额
     */
    @ApiOperation(value = "分摊范围及金额-计算标准")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:edit")
    @PostMapping("/share/equally/money")
    public AjaxResult shareEquallyMoney(@RequestBody ShareEquallyMoneyRequest shareEquallyMoneyRequest) {
        return toAjax(gwjMaintenanceFundsBusinessUrgentAppropriateService.shareEquallyMoney(shareEquallyMoneyRequest));
    }

    /**
     * 修改平摊金额
     */
    @ApiOperation(value = "分摊范围及金额-修改平摊金额")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:edit")
    @PostMapping("/share/equally/money/update")
    public AjaxResult shareEquallyMoneyUpdate(@RequestBody ShareEquallyMoneyRequest shareEquallyMoneyRequest) {
        return toAjax(gwjMaintenanceFundsBusinessUrgentAppropriateService.shareEquallyMoneyUpdate(shareEquallyMoneyRequest));
    }

    /**
     * 应急拨付-查询验收报告
     */
    @ApiOperation(value = "拨付申请-查询验收报告", response = AcceptanceReportDTO.class)
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:list")
    @GetMapping("/get/acceptance/report")
    public AjaxResult getAcceptanceReport(Long bizId) {
        return AjaxResult.success(gwjMaintenanceFundsBusinessUrgentAppropriateService.getAcceptanceReport(bizId));
    }

    /**
     * 保存应急拨付业务
     */
    @ApiOperation(value = "保存拨付申请业务")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:edit")
    @Log(title = "保存拨付申请业务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit/use/apply")
    public AjaxResult saveAppropriateApply(@Validated @RequestBody SaveAppropriateApplyRequest saveAppropriateApplyRequest) {
        return AjaxResult.success(gwjMaintenanceFundsBusinessUrgentAppropriateService.saveAppropriateApply(saveAppropriateApplyRequest));
    }

    /**
     * 分摊金额-选中房屋
     */
    @ApiOperation(value = "分摊范围及金额-选中房屋操作")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:edit")
    @PostMapping("/allocated/amount/arrange")
    public AjaxResult allocatedAmountArrange(@RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        return toAjax(gwjMaintenanceFundsBusinessUrgentAppropriateService.allocatedAmountArrange(allocatedAmountRequest));
    }

    /**
     * 分摊金额-取消选中房屋
     */
    @ApiOperation(value = "分摊范围及金额-取消选中房屋操作")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:edit")
    @PostMapping("/allocated/amount/cancel")
    public AjaxResult allocatedAmountCancel(@RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        return toAjax(gwjMaintenanceFundsBusinessUrgentAppropriateService.allocatedAmountCancel(allocatedAmountRequest));
    }

    /**
     * 拨付申请-查询应急拨付报告
     */
    @ApiOperation(value = "拨付申请-查询申请拨付报告", response = ApplyAppropriateReportDTO.class)
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:list")
    @GetMapping("/get/applyAppropriate/report")
    public AjaxResult getApplyAppropriateReport(Long bizId) {
        return AjaxResult.success(gwjMaintenanceFundsBusinessUrgentAppropriateService.getApplyAppropriateReport(bizId));
    }

    @ApiOperation(value = "业务信息-提交")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:edit")
    @GetMapping("/pre/submit")
    public AjaxResult preSubmit(@RequestParam("bizId") Long bizId) {
        return toAjax(gwjMaintenanceFundsBusinessUrgentAppropriateService.preSubmit(bizId));
    }

    @ApiOperation(value = "应急拨付-查询决算费用分摊清册", response = TotalCostShareListDTO.class)
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:list")
    @GetMapping("/cost/list")
    public AjaxResult costList(Long bizId) {
        startPage();
        return AjaxResult.success(gwjMaintenanceFundsBusinessUrgentAppropriateService.costList(bizId));
    }

    @ApiOperation(value = "查询已选中楼栋列表")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:list")
    @GetMapping("/buildList/{bizId}")
    public TableDataInfo getSelectBuilds(@PathVariable("bizId") Long bizId) {
        // 20230715 取消分页，用于外网显示楼栋列表
        // startPage();
        List<GwjMaintenanceFundsBusinessBuild> selectBuilds = gwjMaintenanceFundsBusinessUrgentAppropriateService.getSelectBuilds(bizId);
        return getDataTable(selectBuilds);
    }

    @ApiOperation(value = "查询楼栋下房屋列表")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:list")
    @PostMapping("/houseList")
    public TableDataInfo getHouseList(@Validated @RequestBody HousesUnderBuildRequest housesUnderBuildRequest) {
        startPage();
        List<GwjMaintenanceFundsBusinessHouse> houseList = gwjMaintenanceFundsBusinessUrgentAppropriateService.getHouseList(housesUnderBuildRequest);
        return getDataTable(houseList);
    }

    @ApiOperation(value = "导出房屋数据")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:export")
    @Log(title = "公维金维修资金业务导出房屋数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        // startPage();
        List<AllocatedAmountListDTO> list = gwjMaintenanceFundsBusinessUrgentAppropriateService.exportHouseList(allocatedAmountRequest);
        ExcelUtil<AllocatedAmountListDTO> util = new ExcelUtil<>(AllocatedAmountListDTO.class);
        util.exportExcel(response, list, "公维金维修资金业务房屋数据");
    }

    @ApiOperation(value = "获取步骤按钮权限信息", response = StepConfigInfoDTO.class)
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:list")
    @GetMapping(value = "/get/step/config/info")
    public AjaxResult getStepConfigInfo() {
        return AjaxResult.success(gwjMaintenanceFundsBusinessUrgentAppropriateService.getStepConfigInfo());
    }

    // ----------------------------------新增功能区 hmh start----------------------------------------
    // 导入20230731
    @Log(title = "公维金支出外网", businessType = BusinessType.IMPORT, systemName = "gwjOutside")
    @RequiresPermissions("gwj:expend:outside:urgent:appropriate:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, @RequestParam(name = "updateSupport") boolean updateSupport, @RequestParam(name = "bizId") Long bizId) throws Exception {
        if (file.isEmpty()) {
            return AjaxResult.error("上传文件不能为空！");
        }
        if (!checkImportFile(file)) {
            return AjaxResult.error("不是指定的xlsx，xls导入类型文件！");
        }
        String result = gwjMaintenanceFundsBusinessService.importHouseList(file, updateSupport, bizId);
        return AjaxResult.success(result);
    }

    /**
     * 检测导入文件类型是否合规
     * 
     * @param file
     * @return
     * @throws IOException
     * @throws IORuntimeException
     */
    private boolean checkImportFile(MultipartFile file) throws Exception {
        // 检查文件类型
        // String type = FileTypeUtil.getType(file.getInputStream());
        String type = FileTypeUtils.getExtension(file);
        // 限制的文件类型集合
        if (StringUtils.inStringIgnoreCase(type, "xlsx", "xls")) {
            return true;
        }
        return false;
    }
    // ----------------------------------新增功能区 hmh end----------------------------------------

}
